package com.luobr.backend.controller;

import com.luobr.backend.annotation.AuthCheck;
import com.luobr.backend.common.BaseResponse;
import com.luobr.backend.common.ResponseCode;
import com.luobr.backend.common.ResultUtils;
import com.luobr.backend.constant.UserConstant;
import com.luobr.backend.exception.ThrowUtils;
import com.luobr.backend.model.dto.analyze.*;
import com.luobr.backend.model.entity.Space;
import com.luobr.backend.model.vo.analyze.*;
import com.luobr.backend.model.vo.user.UserLoginVO;
import com.luobr.backend.service.SpaceAnalyzeService;
import com.luobr.backend.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 罗念笙
 * @date 2025/3/11 17:09
 */
@Slf4j
@RestController
@RequestMapping("/space/analyze")
public class SpaceAnalyzeController {

    @Resource
    private SpaceAnalyzeService spaceAnalyzeService;
    @Resource
    private UserService userService;

    /**
     * 空间资源使用分析
     */
    @PostMapping("/usage")
    public BaseResponse<SpaceUsageAnalyzeResponse> getSpaceUsageAnalyze (@RequestBody SpaceUsageAnalyzeRequest spaceUsageAnalyzeRequest,
                                                           HttpServletRequest request) {
        ThrowUtils.throwIf(spaceUsageAnalyzeRequest == null,ResponseCode.PARAMS_ERROR);
        UserLoginVO loginUser = userService.getLoginUser(request); // 登录用户
        return ResultUtils.success(spaceAnalyzeService.getSpaceUsageAnalyze(spaceUsageAnalyzeRequest,loginUser));
    }

    /**
     * 空间图片分类分析
     */
    @PostMapping("/category")
    public BaseResponse<List<SpaceCategoryAnalyzeResponse>> getSpaceCategoryAnalyze(@RequestBody SpaceCategoryAnalyzeRequest spaceCategoryAnalyzeRequest,
                                                                                    HttpServletRequest request) {
        ThrowUtils.throwIf(spaceCategoryAnalyzeRequest == null, ResponseCode.PARAMS_ERROR);
        UserLoginVO loginUser = userService.getLoginUser(request);
        return ResultUtils.success(spaceAnalyzeService.getSpaceCategoryAnalyze(spaceCategoryAnalyzeRequest, loginUser));
    }

    /**
     * 空间图片标签分析
     */
    @PostMapping("/tag")
    public BaseResponse<List<SpaceTagAnalyzeResponse>> getSpaceTagAnalyze(@RequestBody SpaceTagAnalyzeRequest spaceTagAnalyzeRequest,
                                                                          HttpServletRequest request) {
        ThrowUtils.throwIf(spaceTagAnalyzeRequest == null, ResponseCode.PARAMS_ERROR);
        UserLoginVO loginUser = userService.getLoginUser(request);
        return ResultUtils.success(spaceAnalyzeService.getSpaceTagAnalyze(spaceTagAnalyzeRequest, loginUser));
    }

    /**
     * 空间图片大小分析
     */
    @PostMapping("/size")
    public BaseResponse<List<SpaceSizeAnalyzeResponse>> getSpaceSizeAnalyze(@RequestBody SpaceSizeAnalyzeRequest spaceSizeAnalyzeRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(spaceSizeAnalyzeRequest == null, ResponseCode.PARAMS_ERROR);
        UserLoginVO loginUser = userService.getLoginUser(request);
        return ResultUtils.success(spaceAnalyzeService.getSpaceSizeAnalyze(spaceSizeAnalyzeRequest, loginUser));
    }

    /**
     * 用户行为分析
     */
    @PostMapping("/userAction")
    public BaseResponse<List<SpaceUserActionAnalyzeResponse>> getSpaceUserAnalyze(@RequestBody SpaceUserActionAnalyzeRequest spaceUserActionAnalyzeRequest,
                                                                                  HttpServletRequest request) {
        ThrowUtils.throwIf(spaceUserActionAnalyzeRequest == null, ResponseCode.PARAMS_ERROR);
        UserLoginVO loginUser = userService.getLoginUser(request);
        return ResultUtils.success(spaceAnalyzeService.getSpaceUserActionAnalyze(spaceUserActionAnalyzeRequest, loginUser));
    }

    /**
     * 空间排序分析
     */
    @PostMapping("/rank")
    @AuthCheck(roleValue = UserConstant.USER_ROLE.ADMIN)
    public BaseResponse<List<Space>> getSpaceRankAnalyze(@RequestBody SpaceRankAnalyzeRequest spaceRankAnalyzeRequest) {
        ThrowUtils.throwIf(spaceRankAnalyzeRequest == null, ResponseCode.PARAMS_ERROR);
        return ResultUtils.success(spaceAnalyzeService.getSpaceRankAnalyze(spaceRankAnalyzeRequest));
    }
}